<?php
class WebPageModel extends Model {
	var $_table = 'web_page';
	var $_tplid = '';
	var $page_id_nak; // INT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '页面ID',
	var $deleted_nr = 0; // 是否删除
	var $status_sr = 'draft'; // ENUM('draft','approving','approved',
	// 'published') NOT NULL
	// DEFAULT 'draft' COMMENT '页面状态',
	var $create_uid_nr = 0; // INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '创建用户',
	var $create_time_Tr = 0; // INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '创建时间',
	var $update_uid_nr = 0; // INT UNSIGNED NOT NULL DEFAULT 0,
	var $update_time_Tur = 0; // INT UNSIGNED NOT NULL DEFAULT 0,
	var $cachetime_nr = 0; // INT UNSIGNED NOT NULL DEFAULT 0 COMMENT
	// '缓存时间，单位为秒',
	var $publish_time_r = 0; // INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '发布时间',
	var $page_mode_sr = ''; // VARCHAR(16) NULL COMMENT '页面内容模型',
	var $commentable_nr = 0; // TINYINT(1) UNSIGNED NOT NULL DEFAULT 0 COMMENT
	// '允许评论,0：不允许，1：允许',
	var $title = ''; // VARCHAR(128) NULL COMMENT '页面的标题',
	var $subtitle = ''; // VARCHAR(128) NULL COMMENT '页面的副标题',
	var $category = ''; // VARCHAR(16) NULL COMMENT '栏目',
	var $subcategory = ''; // VARCHAR(16) NULL COMMENT '子栏目',
	var $ontopto_dr = '0000-00-00'; // DATE NULL COMMENT '在该日期以前，一直置顶',
	var $page_type_s = ''; // VARCHAR(16) NULL COMMENT '页面类型',
	var $template_file_s = ''; // VARCHAR(128) NULL COMMENT '模板文件',
	var $keywords = ''; // VARCHAR(256) NULL COMMENT '页面的keywords',
	var $descripition = ''; // VARCHAR(256) NULL COMMENT '页面的描述',
	var $url_slug_s = ''; // char(32) NULL COMMENT 'URL',
	var $url = ''; // VARCHAR(512) NULL COMMENT 'URL',
	var $author = ''; // VARCHAR(32) NULL COMMENT '作者',
	var $source = ''; // VARCHAR(32) NULL COMMENT '来源',
	var $figure = ''; // VARCHAR(128) NULL COMMENT '插图',
	var $summary = '';
	var $content = '';
	
	/**
	 *
	 * @param string $url        	
	 * @return array 解析后的原始URL，可以对应的页面的URL和分页信息.
	 */
	private function parseUrl($url) {
		global $__url, $__cpage;
		$tpl_args = false;
		$url = rtrim ( $url, '/' );
		$urlp = explode ( '/', $url );
		if (preg_match ( '/\.s?html?$/', $url )) {
			$url = explode ( '.', array_pop ( $urlp ) );
			$ext = '.' . array_pop ( $url );
			$urle = implode ( '.', $url );
		} else { //默认主页或带ID的模板页
			$urle = 'index';
			$ext = '.html';
			$tpl_args = $urlp;
		}
		$urls = explode ( '_', $urle );
		if (count ( $urls ) > 1) {
			$__cpage = array_pop ( $urls );
			if (! is_numeric ( $__cpage )) {
				$urls [] = $__cpage;
				$__cpage = 1;
			}
		}
		$urlp [] = implode ( '_', $urls );
		$tpl_url = ltrim ( implode ( '/', $urlp ), '/' );
		$__url = $tpl_url . $ext;
		//生成模板页的url
		if (preg_match ( '#[^/]*\-(.*)#', $tpl_url, $m )) {
			$this->_tplid = $m [1];
			$tpl_url = str_replace ( $m [1], '{args}', $tpl_url ) . $ext;
		} else if ($tpl_args) {
			$_tplid = array_pop ( $tpl_args );
			$_tplid = explode ( '_', $_tplid );
			if (count ( $_tplid ) > 1) {
				$__cpage = array_pop ( $_tplid );
				if (! is_numeric ( $__cpage )) {
					$_tplid [] = $__cpage;
					$__cpage = 1;
				}
			}
			$this->_tplid = implode('_', $_tplid);
			$__url = implode ( '/', $tpl_args );
			$tpl_url = $__url . '/{args}';
			$__url .= '/' . $this->_tplid;
		} else {
			$tpl_url .= '-{args}' . $ext;
		}
		return $tpl_url;
	}
	/**	
	 *
	 * 得到页面信息
	 *
	 * @param string $url        	
	 * @return array 页面信息
	 */
	public function getPageInfo($url, $pid = false, $preview = false) {
		global $__url, $__cpage, $_PAGE;
		$cM = new WebPageCategory ();
		
		$_http_host = $_SERVER ['HTTP_HOST']; //请求域名		
		$domains = explode ( ".", $_http_host );
		$bind = array_shift ( $domains );
		
		if (preg_match ( '#^https?://.+#i', BASE_URL )) {
			$base_host = parse_url ( BASE_URL );
			$base_host = $base_host ['host'];
		} else {
			$base_host = CoreOptionModel::getOption ( "site_domain", 'www' ) . '.' . implode ( ".", $domains );
		}
		$url = ltrim ( $url, '/' );
		if ($_http_host != $base_host && preg_match ( '/^index\.s?html?$/', $url )) { //访问二级域名
			$cate = $cM->where ( array ('bind' => $bind, 'home' => 1 ) )->retrieve ( 'page_id', 0 );
			if ($cate) {
				$pid = $cate ['page_id'];
			} else {
				$pid = apply_filter ( 'get_bind_domain_page', $pid, $bind );
			}
		}
		if (! $pid) {
			$tpl_url = $this->parseUrl ( $url );
			if (! $preview) {
				$where ['status'] = 'published';
				$where ['deleted'] = 0;
			}
			$where ['url_slug'] = md5 ( $__url );
			$page = $this->where ( $where )->retrieve ( '*', 0 );
			if (! $page) { //些时要查找模板页
				$where ['url_slug'] = md5 ( $tpl_url );
				$where ['page_type'] = 'template';
				$page = $this->where ( $where )->retrieve ( '*', 0 );
			}
		} else {
			$page = $this->read ( $pid );
			$this->parseUrl ( $page ['url'] );
		}
		
		if (! $page) {
			return false;
		}
		$page ['content'] = $page ['content'];
		$page ['pageargs'] = $this->_tplid;
		
		$page ['category_name'] = '';
		$page ['category_url'] = '#';
		$page ['crumbs'] = array ();
		if ($page ['category']) {
			$cate = $cM->getCategoryInfo ( $page ['category'] );
			if ($cate) {
				$page ['category_name'] = $cate ['title'];
				$page ['category_url'] = $cate ['url'];
				$page ['category_id'] = $cate ['cid'];
				$page ['bind'] = $cate ['bind'];
				$page ['domain_home'] = $cate ['domain_home'];
				$page ['crumbs'] = $cM->crumb ( 'up_cid', $cate ['cid'], 'title', 'url', 'page_id', 'bind', 'home' );
			}
		}
		if (! empty ( $page ['bind'] ) && $page ['bind'] != $bind) { //强制二级域名访问
			return false;
		}
		$page ['subcategory_name'] = '';
		$page ['subcategory_url'] = '#';
		$page ['subcrumbs'] = array ();
		if ($page ['subcategory']) {
			$cate = $cM->getCategoryInfo ( $page ['subcategory'] );
			if ($cate) {
				$page ['subcategory_name'] = $cate ['title'];
				$page ['subcategory_url'] = $cate ['url'];
				$page ['subcategory_id'] = $cate ['cid'];
				$page ['subcrumbs'] = $cM->crumb ( 'up_cid', $cate ['cid'], 'title', 'url', 'page_id', 'bind' );
			}
		}
		//读取页面的标签
		$tagM = new WebPageTagModel ();
		$page ['tags'] = $tagM->getPageTags ( $page ['page_id'] );
		$page ['flags'] = $tagM->getPageFlags ( $page ['page_id'] );
		// 页面模板
		if (empty ( $page ['template_file'] )) {
			$tM = new WebTemplateModel ();
			$tpl = $tM->getTemplateFile ( $page ['page_type'] );
			if (! $tpl) {
				return false;
			}
			$page ['template_file'] = $tpl;
		}
		// 关键词
		if (empty ( $page ['keywords'] )) {
			$page ['keywords'] = CoreOptionModel::getOption ( 'site_keywords' );
		}
		// 描述
		if (empty ( $page ['descripition'] )) {
			$page ['descripition'] = CoreOptionModel::getOption ( 'site_desc' );
		}
		$page ['cpage'] = $__cpage;
		$_PAGE = $page;
		return $page;
	}
}